/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.sanerzone.smscenter.modules.sms.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sanerzone.smscenter.modules.sms.dao.SmsPhoneWhitelistDao;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.sanerzone.smscenter.modules.sms.service.SmsPhoneWhitelistService;

import com.sanerzone.jeebase.common.config.Global;
import com.sanerzone.jeebase.common.persistence.Page;
import com.sanerzone.jeebase.common.web.BaseController;
import com.sanerzone.jeebase.common.utils.StringUtils;
import com.sanerzone.smscenter.modules.sms.entity.SmsPhoneWhitelistChange;
import com.sanerzone.smscenter.modules.sms.service.SmsPhoneWhitelistChangeService;
import com.sanerzone.smscenter.modules.sms.entity.SmsPhoneWhitelist;
/**
 * 白名单变动Controller
 * @author xql
 * @version 2018-04-17
 */
@Controller
@RequestMapping(value = "${adminPath}/sms/smsPhoneWhitelistChange")
public class SmsPhoneWhitelistChangeController extends BaseController {

	@Autowired
	protected SmsPhoneWhitelistDao smsPhoneWhitelistDao;

	@Autowired
	private SmsPhoneWhitelistService smsPhoneWhitelistService;

	@Autowired
	private SmsPhoneWhitelistChangeService smsPhoneWhitelistChangeService;
	
	@ModelAttribute
	public SmsPhoneWhitelistChange get(@RequestParam(required=false) String id) {
		SmsPhoneWhitelistChange entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = smsPhoneWhitelistChangeService.get(id);
		}
		if (entity == null){
			entity = new SmsPhoneWhitelistChange();
		}
		return entity;
	}
	
	@RequiresPermissions("sms:smsPhoneWhitelistChange:view")
	@RequestMapping(value = {"list", ""})
	public String list(SmsPhoneWhitelistChange smsPhoneWhitelistChange, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SmsPhoneWhitelistChange> page = smsPhoneWhitelistChangeService.findPage(new Page<SmsPhoneWhitelistChange>(request, response), smsPhoneWhitelistChange); 
		model.addAttribute("page", page);
		return "modules/sms/smsPhoneWhitelistChangeList";
	}

	@RequiresPermissions("sms:smsPhoneWhitelistChange:view")
	@RequestMapping(value = "form")
	public String form(SmsPhoneWhitelistChange smsPhoneWhitelistChange, Model model) {
		model.addAttribute("smsPhoneWhitelistChange", smsPhoneWhitelistChange);
		return "modules/sms/smsPhoneWhitelistChangeForm";
	}

	@RequiresPermissions("sms:smsPhoneWhitelistChange:edit")
	@RequestMapping(value = "save")
	public String save(SmsPhoneWhitelistChange smsPhoneWhitelistChange, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, smsPhoneWhitelistChange)){
			return form(smsPhoneWhitelistChange, model);
		}
		smsPhoneWhitelistChangeService.save(smsPhoneWhitelistChange);
		smsPhoneWhitelistDao.updateRemark(smsPhoneWhitelistChange.getId(),smsPhoneWhitelistChange.getRemark());
		addMessage(redirectAttributes, "保存白名单变动成功");
		return "redirect:"+Global.getAdminPath()+"/sms/smsPhoneWhitelistChange/?repage";
	}
	
	@RequiresPermissions("sms:smsPhoneWhitelistChange:edit")
	@RequestMapping(value = "delete")
	public String delete(SmsPhoneWhitelistChange smsPhoneWhitelistChange, RedirectAttributes redirectAttributes) {
		smsPhoneWhitelistChangeService.delete(smsPhoneWhitelistChange);
		smsPhoneWhitelistDao.deleteByChangeid(smsPhoneWhitelistChange.getId());
		addMessage(redirectAttributes, "删除白名单变动成功");
		return "redirect:"+Global.getAdminPath()+"/sms/smsPhoneWhitelistChange/?repage";
	}

}